﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace ServiceOperater.Components
{
    /// <summary>
    /// 来自http://blog.csdn.net/wmjcom/article/details/8505488
    /// </summary>
    public class BindingProxy : Freezable
    {
        #region Overrides of Freezable
        protected override Freezable CreateInstanceCore()
        {
            return new BindingProxy();
        }
        #endregion
        public object Data
        {
            get { return (object)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
    }

    /* 用法  
     1. 在DataGrid/TreeView的资源中通过声明语法来实例化这个类,并且将Data属性binding到当前的DataContext上
     2. 最后就是将这BindingProxy对象作为Source指定给Binding
     3. 不需要TreeViewHelper中SelectedItem的支持.
     <TreeView.Resources>
                <BindingProxy:BindingProxy x:Key="treeviewProxy" Data="{Binding ElementName=treeViewCellSite}"/>
                     <!--这一行就够了-->
                <!-- <BindingProxy:BindingProxy x:Key="treeviewProxy" Data="{Binding}"/>-->
     </TreeView.Resources>
	 
    <ContextMenu x:Key="CellSiteContextMenu" Style="{x:Null}" DataContext="{Binding Source={StaticResource treeviewProxy}, Path=Data.DataContext}">
     *                  // 这里的Data.SelectedItem是写死的
                    <MenuItem Header="{x:Static prop:Resources.Edit}"  Command="{Binding  EditCommand}"  CommandParameter="{Binding Source={StaticResource treeviewProxy}, Path=Data.SelectedItem}"  InputGestureText="{x:Static prop:Resources.ShortcutEdit}"/>
     </ContextMenu>
	 
	 
	 
	 
	 
	 
	 
	 
	 
     */
}
